package com.csxy.six.controller;

import com.csxy.six.dto.ClaimRequestDTO;
import com.csxy.six.service.ClaimService;
import com.csxy.six.util.Result;
import com.csxy.six.util.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/claims")
public class ClaimController {

    @Autowired
    private ClaimService claimService;

    @PostMapping
    public Result<Void> requestClaim(@RequestBody ClaimRequestDTO claimRequestDTO) {
        try {
            Long userId = SecurityUtil.getCurrentUserId();
            if (userId == null) {
                return Result.fail("用户未登录");
            }
            if (claimService.requestClaim(claimRequestDTO, userId)) {
                return Result.success();
            }
            return Result.fail("提交认领请求失败");
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }

    @PutMapping("/{id}/process")
    public Result<Void> processClaim(@PathVariable Integer id,
                                     @RequestParam boolean approved,
                                     @RequestParam(required = false) String remark) {
        try {
            Long adminId = SecurityUtil.getCurrentUserId();
            if (claimService.processClaim(id, approved, remark, adminId)) {
                return Result.success();
            }
            return Result.fail("处理认领请求失败");
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }
}